// 倒计时组件
Vue.component('countdown', {
    props: ['seconds', 'index'],
    data: () => {
         return {
            timerCount: 0
         }
     },
     mounted() {
         this.timing();
     },
     methods: {
         timing() {
             let startTime = localStorage.getItem(`startTime${this.index}`);
             let nowTime = new Date().getTime();
             if(startTime) {
                  let surplus = this.seconds - parseInt((nowTime-startTime) / 1000, 10);
                  this.timerCount = surplus <= 0 ? 0 : surplus;
             } else {
                  this.timerCount = this.seconds;
                  localStorage.setItem(`startTime${this.index}`, nowTime);
             }

             let timer = setInterval(() => {
                  if(this.timerCount > 0 && this.timerCount <= this.seconds) {
                      this.timerCount--;
             　　  } else {
                      localStorage.removeItem(`startTime${this.index}`);
                      clearInterval(timer);
                  }
             }, 1000);
         }
    },
    template: `<div class="box">倒计时 <span class="count-number">{{timerCount < 10 ? '0' + timerCount : timerCount}}</span> 秒</div>`
});

// Vue实例
new Vue({
    el: '.wrap'
});